package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;

/**
 * 会员统计服务
 * @author zhengjd
 * @date 2020/4/12 9:47
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private ReportService reportService;


    /**
     * 会员统计折线图
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            //获取当前日期的前12个月
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12);
            ArrayList<String> list = new ArrayList<>();
            for (int i = 0; i < 12; i++) {
                calendar.add(Calendar.MONTH,1);
                list.add(DateUtils.parseDate2String(calendar.getTime(),"yyyy-MM"));

            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("months",list);
            List<Integer> memberCount = reportService.getMemberCountByMonth(list);
            map.put("memberCount",memberCount);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);

        }
    }

    /**
     * 统计套餐预约占比饼状图
     * @return
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            //获取所有套餐和套餐数量
            Map map = reportService.getSetmealReport();

            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);

        }
    }

    /**
     * 运营数据统计
     * @return
     */
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map map = reportService.getBusinessReportData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);

        }
    }

    /**
     * 导出数据运营统计模板
     * @return
     */
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        try {
            Map map = reportService.getBusinessReportData();
            String reportDate =(String) map.get("reportDate");
            long todayNewMember =(long) map.get("todayNewMember");
            long totalMember =(long) map.get("totalMember");
            long thisWeekNewMember =(long) map.get("thisWeekNewMember");
            long thisMonthNewMember =(long) map.get("thisMonthNewMember");
            long todayOrderNumber =(long) map.get("todayOrderNumber");
            long todayVisitsNumber =(long) map.get("todayVisitsNumber");
            long thisWeekOrderNumber =(long) map.get("thisWeekOrderNumber");
            long thisWeekVisitsNumber =(long) map.get("thisWeekVisitsNumber");
            long thisMonthOrderNumber =(long) map.get("thisMonthOrderNumber");
            long thisMonthVisitsNumber =(long) map.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal =(List<Map>) map.get("hotSetmeal");

            //获取模板的绝对路径
            String template = request.getSession().getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
            //创建workbook对象
            XSSFWorkbook workbook = new XSSFWorkbook(template);
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            //设置日期
            XSSFRow row = sheetAt.getRow(2);
            row.getCell(5).setCellValue(reportDate);
            //设置会员数
            row = sheetAt.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            row.getCell(7).setCellValue(totalMember);
            row = sheetAt.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            row.getCell(7).setCellValue(thisMonthNewMember);

            //设置预约到诊数据
            row = sheetAt.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            row.getCell(7).setCellValue(todayVisitsNumber);
            row = sheetAt.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            row.getCell(7).setCellValue(thisWeekVisitsNumber);
            row = sheetAt.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            row.getCell(7).setCellValue(thisMonthVisitsNumber);

            //设置热门套餐数
            int rowNum = 12;
            for(Map setmeal : hotSetmeal){//热门套餐
                String name = (String) setmeal.get("name");
                Long setmeal_count = (Long) setmeal.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) setmeal.get("proportion");
                row = sheetAt.getRow(rowNum ++);
                row.getCell(4).setCellValue(name);//套餐名称
                row.getCell(5).setCellValue(setmeal_count);//预约数量
                row.getCell(6).setCellValue(proportion.doubleValue());//占比
            }

            //通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            workbook.write(out);

            out.flush();
            out.close();
            workbook.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);

        }
    }



}
